package com.ruoyi.web.controller.course;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.domain.StuCourse;
import com.ruoyi.course.service.StuCourseService;
import com.ruoyi.major.domain.StuMagor;
import com.ruoyi.major.service.IStuMagorService;
import com.ruoyi.stage.domain.StuStage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/course/list")
public class StuCourseController extends BaseController {

    @Autowired
    private StuCourseService stuCourseService;

    @Resource
    private IStuMagorService stuMagorService;
    /**
     *获取课程列表
     */
    @PreAuthorize("@ss.hasPermi('course:list:list')")
    @GetMapping("/list")
    public TableDataInfo list(StuCourse course)
    {
        startPage();
        List<StuCourse> list = stuCourseService.selectCourseList(course);
        return getDataTable(list);
    }



    @Log(title = "课程列表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('course:show:export')")
    @PostMapping ("/export")
    public void export(HttpServletResponse response, StuCourse course)
    {
        List<StuCourse> list = stuCourseService.selectCourseList(course);
        ExcelUtil<StuCourse> util = new ExcelUtil<StuCourse>(StuCourse.class);
        util.exportExcel(response, list, "岗位数据");
    }

    @GetMapping(value = { "/", "/{courId}" })
    public AjaxResult getInfo(@PathVariable(value = "courId", required = false) Long courId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("magOptions", stuMagorService.selectAllMagorList());
        if (StringUtils.isNotNull(courId))
        {
            StuCourse stuCourse = stuCourseService.selectCourseById(courId);
            ajax.put(AjaxResult.DATA_TAG, stuCourse);
            ajax.put("magIds", stuMagorService.selectMagorByCourId(courId));
        }
        return ajax;
    }



    @PostMapping
    public AjaxResult add(@RequestBody StuCourse stuCourse)
    {

        stuCourse.setCreateBy(getUsername());
        return toAjax(stuCourseService.insertStuCourse(stuCourse));
    }

    /**
     * 修改专业
     * @param stuCourse
     * @return
     */
    @PutMapping
    @PreAuthorize("@ss.hasPermi('cour:courlist:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated @RequestBody StuCourse stuCourse)
    {
//        if (UserConstants.NOT_UNIQUE.equals(stuMagorService.checkMajorNameUnique(stuMagor)))
//        {
//            return AjaxResult.error("修改岗位'" + stuMagor.getMagName() + "'失败，专业名称已存在");
//        }
        stuCourse.setUpdateBy(getUsername());
        return toAjax(stuCourseService.updateStuCourse(stuCourse));
    }

    /**
     * 删除专业
     */
    @DeleteMapping("/{courIds}")
    @PreAuthorize("@ss.hasPermi('course:courselist:remove')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] courIds)
    {
        return toAjax(stuCourseService.deleteCourseByIds(courIds));
    }



}
